package com.example.springbatch_step_condition;

import com.example.springbatch_step_chuck.MyItemWriter;
import com.example.springbatch_step_chuck.MyStepListener;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.*;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/***
 *
 */
@SpringBootApplication
@EnableBatchProcessing //启用springbatch框架 需要的注解
public class SpringBatchApplication8 {

    // job调度器
    @Autowired
    private JobLauncher jobLauncher;

    // job构建工厂
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    // step步骤构建工厂
    @Autowired
    private StepBuilderFactory stepBuilderFactory;



    int timer = 10;


    /**
     * 读操作
     * 需要注意的是 只有返回null的时候，读操作才会结束，否则会一直循环进行
     * @return 返回读数据
     */
    @Bean
    public ItemReader<String> itemReader() {

        return new ItemReader<String>() {
            @Override
            public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
                int record = timer;
                if (timer -- > 0) {
                    return "read-raw-data : " + record + "\n";

                } else {
                    return null;
                }

            }
        };

    }

    /**
     * 数据处理
     *
     * @return 处理后的数据
     */
    @Bean
    public ItemProcessor<String, String> itemProcessor() {

        return new ItemProcessor<String, String>() {
            @Override
            public String process(String rawData) throws Exception {
                return "data-processed => " + rawData;
            }
        };

    }


    @Bean
    public Tasklet firstTasklet() {
        return (stepContribution, chunkContext) -> {
            System.out.println("------ first Tasklet ------");

//            int i = 1 / 0 ; //模拟异常 执行异常错误

            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }


    @Bean
    public Tasklet successTasklet() {
        return (stepContribution, chunkContext) -> {

            System.out.println("------ success Tasklet ------");

            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet failTasklet() {
        return (stepContribution, chunkContext) -> {
            System.out.println("------ fail Tasklet ------");

            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }


    // 作业步骤 不带不读写操作
    @Bean
    public Step firstStep() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("firstStep")
                .tasklet(firstTasklet())
                .build();
    }

    @Bean
    public Step successStep() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("successStep")
                .tasklet(successTasklet())
                .build();
    }


    @Bean
    public Step failStep() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("failStep")
                .tasklet(failTasklet())
                .build();
    }



    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("step-chuck-condition")
                .start(firstStep())
                .on("FAILED").to(failStep())
                .from(firstStep())
                .on("*").to(successStep()).end()
                .incrementer(new RunIdIncrementer())    // 使用增量器(run.id自增)
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBatchApplication8.class, args);
    }

}
